package exampleQuestions;

public class LongestPalindromOfAString {

	/**
	 * Longest palindrome of the string
	 */
	public static void main(String[] args) {

		final String input = "bcdakobylamamalybokcfguieopws";
		
		System.out.println(getLongestPalindrom(input));
	}

	private static int getLongestPalindrom(String input) {
		
		int length = 1;
		int start = 0;
		int end = 0;
		
		if(input == null || input.length() == 0) {
			length = 0;
			
		} else {
			
			for (int i=1; i<input.length(); i++) {
				int currentMax = 1;
				int j=0;
				while(i-j-1 >= 0 && i+j+1 < input.length() && input.charAt(i-j-1) == input.charAt(i+j+1)) {
					currentMax +=2;
					j++;
				}
				if(currentMax > length) {
					length = currentMax;
					start = i-j;
					end = i+j;
				}
			}
			
			for (int i=0; i<input.length()-1; i++) {
				
				if(input.charAt(i) == input.charAt(i+1)) {
					int currentMax = 2;
					int j=0;
					while(i-j-1 >= 0 && i+j+2 < input.length() && input.charAt(i-j-1) == input.charAt(i+j+2)) {
						currentMax +=2;
						j++;
					}
					if(currentMax > length) {
						length = currentMax;
						start = i-j;
						end = i+j+1;
					}
				}
			}
			
			System.out.println(input.substring(start, end+1));
		}
		return length;
		
	}

}
